from rest_framework.permissions import BasePermission
from .exceptions import JsonAPIException
from . import ErrCode


class IsSuperUser(BasePermission):
    def has_permission(self, request, view):
        from django.contrib.auth.models import User as DjangoUser
        try:
            user = DjangoUser.objects.get(pk=request.user.id)
        except DjangoUser.DoesNotExist:
            raise JsonAPIException(code=ErrCode.PERMISSION_DENY.name, msg='请登录', status=401)
        if user.is_staff and user.is_active and user.is_superuser:
            return True
        else:
            raise JsonAPIException(code=ErrCode.PERMISSION_DENY.name, msg=ErrCode, status=401)
